package com.lili.math;

/**
 * @Auther: 李 力
 * @Date: 2024/8/5
 * @Description: 最大交换
 * @version: 1.0
 */
public class LeetCode670 {
    /*
     * 时间复杂度: O(log^3num)
     * 空间复杂度: O(lognum)
     *
     * */
    public static int maximumSwap(int num) {
        char[] charArray = String.valueOf(num).toCharArray();
        int n = charArray.length;
        int maxNUm = num;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                swap(charArray, i, j);
                maxNUm = Math.max(maxNUm, Integer.parseInt(new String(charArray)));
                //记得换回来
                swap(charArray, i, j);
            }
        }
        return maxNUm;
    }

    private static void swap(char[] charArray, int i, int j) {
        char temp = charArray[i];
        charArray[i] = charArray[j];
        charArray[j] = temp;
    }
}
